<template>
	<view>
		<u-sticky>
			<u-tabs :list="list" :is-scroll="false" :current="current" @change="sectionChange"></u-tabs>
		</u-sticky>
		<u-card padding="16" v-for="(item,index) in dataList" :key="index" v-if="current == 0">
			<view class="" slot="head">
				<view class="wrap">
					<u-row>
						<u-col span="9" text-align="left">
							<view class="u-line-1 weikaishi2">
								<span>车次：{{item.trainNumber}}</span>
							</view>
						</u-col>
						<u-col span="3" text-align="right" v-if="item.tstate == '已定价'">
							<view class=" weikaishi3">
								<span>待装车</span>
							</view>
						</u-col>
						<u-col span="3" text-align="right" v-if="item.tstate == '待发运'">
							<view class=" weikaishi3">
								<span>待发运</span>
							</view>
						</u-col>
						<u-col span="3" text-align="right" v-if="item.tstate == '运输中'">
							<view class=" weikaishi3 jinxingzhong">
								<span>运输中</span>
							</view>
						</u-col>
						<u-col span="3" text-align="right" v-if="item.tstate == '已完成'">
							<view class=" weikaishi3 yiwancheng">
								<span>已完成</span>
							</view>
						</u-col>
					</u-row>
					<u-row>
						<u-col span="10" text-align="left">
							<view class="weikaishi3 lanse">
								<span>运单号：{{item.dispatchNumber}}</span>
							</view>
						</u-col>
						<u-col span="2" text-align="right">
							<view class=" weikaishi3 jinxingzhong" @click="etcSign(item.sginUrl)"
								v-if="item.etcSignStat == '签署'">
								<span>签署</span>
							</view>
							<view class=" weikaishi3 jinxingzhong" @click="electronsign(item.dispatchNumber)"
								v-if="item.etcSignStat == '电子签'">
								<span>电子签</span>
							</view>
						</u-col>
					</u-row>
					<u-row>
						<u-col span="7" text-align="left">
							<view class="u-line-1 weikaishi4">
								<span>发车日期：{{item.startDate}}</span>
							</view>
						</u-col>
						<u-col span="5" text-align="left">
							<view class="u-line-1 u-m-t-5" v-if="item.zhuangzaiFileid"
								@click="viewImage(item.zhuangzaiFileid)">
								<u-icon name="file-text" label="装载照片" label-color="#2b85e4" color="#2b85e4" size="30">
								</u-icon>
							</view>
							<view class="u-line-1 u-m-t-5" v-else @click="uploadimg(item.dispatchNumber)">
								<u-icon name="file-text" label="装载照片未传" label-color="#e84d74" color="#e84d74" size="30">
								</u-icon>
							</view>
						</u-col>
						<u-col span="7" text-align="left">
							<view class=" u-m-t-2 weikaishi4">
								<span>总体积：{{item.tooDeployVolume}}m³</span>
							</view>
						</u-col>
						<u-col span="5" text-align="left">
							<view class=" u-m-t-2 weikaishi4">
								<span>总件数：{{item.pieceNumberSum}}</span>
							</view>
						</u-col>
					</u-row>
				</view>
			</view>
			<view class="" slot="body">
				<view class="wrap">
					<u-row>
						<u-col span="12" text-align="center">
							<view class="u-line-1 u-m-t-2">
								<u-line-progress active-color="#19be6b" :striped="true"
									:percent="(item.tooDeployVolume/item.privatecarSurevolume*100).toFixed(1)"
									:striped-active="true"></u-line-progress>
							</view>
						</u-col>
					</u-row>
					<u-collapse>
						<u-collapse-item title="详情">
							<view v-for="(item1,index1) in item.list" :key="index1">
								<u-row>
									<u-col span="9" text-align="left">
										<view class="weikaishi4">
											<span>
												{{item1.getUnit}}
												<u-icon name="search" label="注意事项" label-color="#2b85e4" color="#2b85e4"
													size="30" @click="showCareful(item1.getUnitId)"></u-icon>
											</span>
										</view>
									</u-col>
									<u-col span="3" text-align="center">
										<view class="u-line-1" @click="badaphone(item1.getTel)">
											<u-icon name="phone" :label="item1.getPeople" label-color="#2b85e4"
												color="#2b85e4" size="30"></u-icon>
										</view>
									</u-col>
									<u-col span="9" text-align="left">
										<view class=" u-m-t-2 weikaishi5">
											<span>
												<u-icon name="map" size="29" labelSize="14px" labelColor="#00c300"
													label="地址"></u-icon>：{{item1.getLocation}}
											</span>
										</view>
									</u-col>
									<u-col span="3" text-align="center">
										<view class="u-line-1"
											@click="toMap(item1.getLocation,item1.getUnit,item1.longitude2,item1.latitude2)">
											<u-icon name="map" label="到这去" label-color="#2b85e4" color="#2b85e4"
												size="35"></u-icon>
										</view>
									</u-col>
									<u-col span="6" text-align="left">
										<view class="u-line-1 u-m-t-2">
											<span>数量：{{item1.totalNum}}</span>
										</view>
									</u-col>
									<u-col span="6" text-align="left">
										<view class="u-line-1 u-m-t-2">
											<span>体积：{{item1.tooVolume}}m³</span>
										</view>
									</u-col>
									<u-col span="12" text-align="right">
										
										<view class="u-m-t-25 weikaishi5"  @click="yanshou(item.dispatchNumber,item1.xlbClientId)">
											门店验收码
										</view>
									</u-col>
								</u-row>
							</view>
						</u-collapse-item>
					</u-collapse>
				</view>
			</view>
			<view class="foot-butten" slot="foot">
				<view :class="flex" v-if="item.tstate != '运输中'">
					<u-col span="4" text-align="center" @click="tooCarsy(item.dispatchNumber)">
						<view class="u-m-10 u-border-right">
							配置详情
						</view>
					</u-col>
					<u-col span="4" text-align="center" @click="toyunshu(item.dispatchNumber)">
						<view class="u-m-10 u-border-right">
							运输信息
						</view>
					</u-col>
					<u-col span="4" text-align="center" @click="daochangzhuangche(item)" v-if="item.tstate == '已定价'">
						<view class="u-m-10 lanse">
							到仓装车
						</view>
					</u-col>
					<u-col span="4" text-align="center"
						@click="lichangfayun(item.dispatchNumber,item.carnumbger,item.driver1,item.cartel,item.guiji)"
						v-if="item.tstate == '待发运'">
						<view class="u-m-10 lanse">
							离场发运
						</view>
					</u-col>
				</view>
				<view :class="flex" v-if="item.tstate == '运输中'">
					<u-col span="2" text-align="center" @click="tooCarsy(item.dispatchNumber)">
						<view class="u-m-10 u-border-right">
							配置详情
						</view>
					</u-col>
					<u-col span="2" text-align="center" @click="toyunshu(item.dispatchNumber)">
						<view class="u-m-10 u-border-right">
							运输信息
						</view>
					</u-col>
					<u-col span="2" text-align="center" @click="songda(item)">
						<view class="u-m-10 lanse u-border-right">
							送达
						</view>
					</u-col>
					<u-col span="2" text-align="center" @click="huidan(item.dispatchNumber)">
						<view class="u-m-10 lanse u-border-right">
							客户签收
						</view>
					</u-col>
					<u-col span="2" text-align="center"
						@click="wancheng(item.dispatchNumber,item.carnumbger,item.fayunTimes)">
						<view class="u-m-10 lanse">
							完成
						</view>
					</u-col>
					<u-col span="2" text-align="center" @click="selfPrint(item)">
						<view class="u-m-10">
							自助打印
						</view>
					</u-col>
				</view>
			</view>
		</u-card>
		<u-card padding="16" v-for="(item,index) in dataList2" :key="index" v-if="current == 1">
			<view class="" slot="head">
				<view class="wrap">
					<u-row>
						<u-col span="9" text-align="left">
							<view class="u-line-1 weikaishi2">
								<span>车次：{{item.trainNumber}}</span>
							</view>
						</u-col>
						<u-col span="3" text-align="right">
							<view class="u-line-1 weikaishi3 yiwancheng">
								<span>已完成</span>
							</view>
						</u-col>
						<u-col span="12" text-align="left">
							<view class="u-line-1 weikaishi3 lanse">
								<span>运单号：{{item.dispatchNumber}}</span>
							</view>
						</u-col>
					</u-row>
					<u-row>
						<u-col span="6" text-align="left">
							<view class="u-line-1">
								<u-icon name="car-fill" :label="item.carnumbger" label-size="33" label-color="#19be6b"
									color="#19be6b" size="45"></u-icon>
							</view>
						</u-col>
						<u-col span="6" text-align="left">
							<view class="u-line-1 u-m-t-5" @click="huidan(item.dispatchNumber)">
								<u-icon name="file-text" label="回单" label-color="#2b85e4" color="#2b85e4" size="30">
								</u-icon>
							</view>
						</u-col>
					</u-row>
					<u-row>
						<u-col span="6" text-align="left">
							<view class="u-line-1 u-m-t-5">
								<span>{{item.privatecarType}}</span>
							</view>
						</u-col>
						<u-col span="6" text-align="left">
							<view class="u-line-1 u-m-t-5" @click="songda(item)">
								<u-icon name="file-text" label="送达签名" label-color="#2b85e4" color="#2b85e4" size="30">
								</u-icon>
							</view>
						</u-col>
					</u-row>
					<u-row>
						<u-col span="7" text-align="left">
							<view class="u-line-1 weikaishi4">
								<span>发车日期：{{item.startDate}}</span>
							</view>
						</u-col>
						<u-col span="5" text-align="left">
							<view class="u-line-1 u-m-t-5" v-if="item.zhuangzaiFileid"
								@click="viewImage(item.zhuangzaiFileid)">
								<u-icon name="file-text" label="装载照片" label-color="#2b85e4" color="#2b85e4" size="30">
								</u-icon>
							</view>
							<view class="u-line-1 u-m-t-5" v-else @click="uploadimg(item.dispatchNumber)">
								<u-icon name="file-text" label="装载照片未传" label-color="#e84d74" color="#e84d74" size="30">
								</u-icon>
							</view>
						</u-col>
						<u-col span="7" text-align="left">
							<view class=" u-m-t-2 weikaishi4">
								<span>总体积：{{item.tooDeployVolume}}m³</span>
							</view>
						</u-col>
						<u-col span="5" text-align="left">
							<view class=" u-m-t-2 weikaishi4">
								<span>总件数：{{item.pieceNumberSum}}</span>
							</view>
						</u-col>
					</u-row>
				</view>
			</view>
			<view class="" slot="body">
				<view class="wrap">
					<u-row>
						<u-col span="12" text-align="center">
							<view class="u-line-1 u-m-t-2">
								<u-line-progress active-color="#19be6b" :striped="true"
									:percent="(item.tooDeployVolume/item.privatecarSurevolume*100).toFixed(1)"
									:striped-active="true"></u-line-progress>
							</view>
						</u-col>
					</u-row>
					<u-collapse>
						<u-collapse-item title="详情">
							<view v-for="(item1,index1) in item.list" :key="index1">
								<u-row>
									<u-col span="9" text-align="left">
										<view class="weikaishi4">
											<span>{{item1.getUnit}}</span>
										</view>
									</u-col>
									<u-col span="3" text-align="center">
										<view class="u-line-1 u-m-t-5" @click="badaphone(item1.getTel)">
											<u-icon name="phone" :label="item1.getPeople" label-color="#2b85e4"
												color="#2b85e4" size="30"></u-icon>
										</view>
									</u-col>
									<u-col span="12" text-align="left">
										<view class=" u-m-t-2 weikaishi5">
											<span>
												<u-icon name="map" size="29" labelSize="14px" labelColor="#00c300"
													label="地址"></u-icon>：{{item1.getLocation}}
											</span>
										</view>
									</u-col>
									<u-col span="6" text-align="left">
										<view class="u-line-1 u-m-t-2">
											<span>体积{{item1.tooVolume}}m³</span>
										</view>
									</u-col>
									<u-col span="6" text-align="left">
										<view class="u-line-1 u-m-t-2">
											<span>数量{{item1.totalNum}}</span>
										</view>
									</u-col>
									<u-col span="12" text-align="right">
										<view class="u-m-t-25 u-line-1">
										</view>
									</u-col>
								</u-row>
							</view>
						</u-collapse-item>
					</u-collapse>
				</view>
			</view>
			<view class="foot-butten" slot="foot">
				<view :class="flex">
					<u-col span="4" text-align="center" @click="tooCarsy(item.dispatchNumber)">
						<view class="u-m-10 u-border-right">
							配置详情
						</view>
					</u-col>
					<u-col span="4" text-align="center" @click="toyunshu(item.dispatchNumber)">
						<view class="u-m-10 ">
							运输信息
						</view>
					</u-col>
					<u-col span="4" text-align="center" @click="tojypj(item.dispatchNumber)">
						<view class="u-m-10 ">
							评价建议
						</view>
					</u-col>
				</view>
			</view>
		</u-card>
		<u-popup v-model="show" mode="center" closeable width="650rpx" height="850rpx" border-radius="10">
			<view class="u-text-left u-m-t-20 weikaishi2" style="margin-left: 20rpx;margin-bottom: 30rpx;">选择送达客户</view>
			<view class="wrap">
				<view style="background-color: #eaeaea;" v-for="(item,index) in clist" :key="index">
					<u-row @click="xuznekehu(item)">
						<u-col span="12" text-align="left">
							<view class=" u-m-t-10 weikaishi lanse">
								<span>{{item.getUnit}}</span>
							</view>
						</u-col>
						<u-col span="12" text-align="left">
							<view class=" u-m-t-10 weikaishi4">
								<span>地址：{{item.getLocation}}</span>
							</view>
						</u-col>
					</u-row>
				</view>
			</view>
		</u-popup>
		<u-modal v-model="show3" :title="title" :content='content'></u-modal>
		<view>
			<u-toast ref="uToast" />
		</view>
		<!-- 条形码 -->
		<u-popup v-model="barCode" mode="center" :mask="true" :mask-custom-style="{ background: 'rgba(0, 0, 0, 1)' }">
			<w-barcode :options="options"></w-barcode>
		</u-popup>
		
		<!-- 条形码 -->
		<u-popup v-model="ysmShow" mode="center" :mask="true" width="460rpx" height="460rpx">
			<view class="qrcode">
				<canvas canvas-id="qrcode" id="qrcode" />
			</view>
		</u-popup>

	</view>
</template>

<script>
	import bmap from '../../static/js/bmap-wx.js'
	import config from '../../config.js'
	import CODE  from '@uni-plugs/uni-code'

	export default {
		data() {
			return {
				urlShow: false,
				webViewUrl: "https://test.essurl.cn/dzrgUBJ0cc",
				barCode: false,
				options: {
					width: 1000,
					height: 400,
					// color: ['#45B649','#00c3ff', '#ee0979'],
					code: '',
					orient: 'vertical',
					text: {
						content: '',
						color: ['#ee0979', '#e96443', '#904e95'],
						position: 'top', //bottom
						size: 40,
						padding: 40
					}
				},
				ysmShow: false,
				ysmOptions: {
					width: 420,
					height: 420,
					code: '',
					orient: 'vertical',
					text: {
						content: '',
						color: ['#ee0979', '#e96443', '#904e95'],
						position: 'top', //bottom
						size: 40,
						padding: 40
					}
				},
				flex: '',
				list: [{
						name: '配送中'
					},
					{
						name: '已完成'
					}
				],
				current: 0,
				dataList: [],
				dataList2: [],
				clist: [],
				show: false,
				show2: false,
				show3: false,
				title: '注意事项',
				content: '',
				pageNo: 1,
				hasMore: true,
				orderCount: 0,
				basicCustomer: {
					isEtcSign: '是'
				},
				BMap: {},
			}
		},
		onLoad() {
			//#ifdef H5 || APP-PLUS
			this.flex = 'u-flex'
			//#endif

			// 新建百度地图对象
			//#ifdef MP-WEIXIN
			this.BMap = new bmap.BMapWX({
				ak: 'ESWpVgDaBgDk7KWsgkUkBE6kzAN0BKTa'
			});
			//#endif

			//注意:将代码放在onLoad方法下
			//#ifdef MP-WEIXIN
			if (!wx.getStorageSync('hadAuthed')) {
				wx.login({
					success(res) {
						if (res.code) {
							uni.request({
								method: "POST",
								header: {
									'Authorization': uni.getStorageSync('token'),
								},
								url: config.root + '/rest/transport/getopenId?code=' + res.code,
								data: {},
								success: (res) => {
									if (res.data.code == 200) {
										if (res.data.data != null && res.data.data != '') {
											wx.setStorageSync('openId', res.data.data)
											wx.setStorageSync('hadAuthed', true)
										}
									}
								}
							})
						} else {
							console.log('登录失败！' + res.errMsg)
						}
					}
				})
			}
			//#endif

			this.refresh()
		},
		onShow() {},
		onPullDownRefresh() {
			this.refresh()
			uni.stopPullDownRefresh()
		},
		onReachBottom() {
			if (this.current == 1) {
				uni.showLoading({
					title: '加载中'
				})
				if (this.hasMore) {
					this.pageNo++
					setTimeout(() => {
						this.loadOrder({
							findtype: '1'
						}).then(res => {})
						uni.hideLoading()
					}, 500)
				} else {
					setTimeout(() => {
						uni.hideLoading()
						uni.showToast({
							icon: 'none',
							title: '已经到低了哦',
							duration: 2000
						});
					}, 500)
				}
			}
		},
		methods: {
			edittDispatchYy(dispatchNumber, mileageTraveledYy) {
				uni.request({
					method: "POST",
					url: config.root + '/rest/transport/edittDispatchYy?dispatchNumber=' + dispatchNumber +
						"&mileageTraveledYy=" + mileageTraveledYy,
					data: {},
					header: {
						'Authorization': uni.getStorageSync('token'),
					},
					success: (res) => {
						if (res.data.code == 200) {
							//成功更新
						}
					}
				})
			},
			createUnix(str) {
				var new_str = str.replace(/:/g, '-')
				new_str = new_str.replace(/ /g, '-')
				var arr = new_str.split("-")
				var datum = new Date(Date.UTC(arr[0], arr[1] - 1, arr[2], arr[3] - 8, arr[4], arr[5]))
				return datum.getTime() / 1000
			},
			etcSign(path) {
				wx.navigateToMiniProgram({
					appId: 'wxa023b292fd19d41d', // 电子签appId; 联调时, 请使用demo小程序appId: 'wx371151823f6f3edf' 正式 wxa023b292fd19d41d
					path: path, // 跳转的页面路径，可选，默认跳转到目标小程序首页; 签署时，需使用后台API返回的完整链接（类似pages/guide?id=xxx&foo=bar）
					envVersion: 'release', // 跳转正式或demo小程序，都需要传 'release'
					success(res) {
						// 成功跳转到目标小程序后的回调函数
					},
					fail(res) {
						// 跳转失败的回调函数
					}
				})
			},
			yanshou(dispatchNumber,xlbClientId) {
				let that = this;
				this.$http.post('/rest/transport/qianshouerweima', {}, {
					params: {
						dispatchNumber: dispatchNumber,
						xlbClientId : xlbClientId
					},
					header: {
						'Authorization': uni.getStorageSync('token'),
					}
				}).then(res => {
					if (res.data.code == 200) {
						if(res.data.data){
							CODE.QRCode({
								id: 'qrcode',// canvas的canvas-id
								code: res.data.data,//生成二维码的字符串
								size: 210,// 二维码大小
								level: 4,//等级 0～4
								border:{
									color: ['#e66465','#9198e5'],//边框颜色支持渐变色
									lineWidth: 1,//边框宽度
								},
								color: '#000'// 二维码颜色 默认黑色
							});
							that.ysmShow = true;
						}
					} else {
						console.log(res);
						uni.showModal({
							title: '提示',
							content: res.data.message,
							success: (res) => {
								console.log('用户点击取消');
							}
						});
					}
				})
			},
			
			
			viewImage(fileId) {
				let arr = []
				let img = config.root + 'system/previewa/' + fileId
				arr.push(img)
				uni.previewImage({
					urls: arr,
				});
			},
			showCareful(getUnitId) {
				this.$http.post('/rest/transportCar/findCareful', {}, {
					params: {
						id: getUnitId
					},
					header: {
						'Authorization': uni.getStorageSync('token'),
					}
				}).then(res => {
					this.content = res.data.data.putgoodsNotes
					this.show3 = true
				})
			},
			toMap(location, unit, longitude, latitude) {
				//#ifdef MP-WEIXIN
				if (location == null || location == '') {
					uni.showToast({
						title: "暂无地址",
						icon: 'none'
					})
				} else {
					uni.openLocation({
						latitude: latitude,
						longitude: longitude,
						scale: 15,
						name: unit,
						address: location
					})
				}
				//#endif
			},
			/* 电子签 */
			electronsign(dispatchNumber) {
				uni.showModal({
					content: '确定电子签收吗？',
					success: (res) => {
						if (res.confirm) {
							this.$http.post('/rest/transport/dianziqianyue', {}, {
								params: {
									dispatchNumber: dispatchNumber
								},
								header: {
									'Authorization': uni.getStorageSync('token'),
								}
							}).then(res => {
								if (res.data.code == 200) {
									this.fayun(dispatchNumber);
								} else {
									console.log(res);
									uni.showModal({
										title: '提示',
										content: res.data.message,
										success: (res) => {
											if (res.confirm) {
												this.fayun(dispatchNumber);
											} else if (res.cancel) {
												console.log('用户点击取消');
											}
										}
									});
								}
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				})
			},
			wancheng(dispatchNumber, carnumbger, fayunTimes) {
				uni.showModal({
					content: '确定完成吗？',
					success: (res) => {
						if (res.confirm) {
							uni.showLoading({
								title: '正在提交'
							})
							this.$http.post('/rest/transportCar/accomplishUnitGoods', {}, {
								params: {
									dispatchNumber: dispatchNumber,
								},
								header: {
									'Authorization': uni.getStorageSync('token'),
								}
							}).then(res => {
								uni.hideLoading()
								if (res.data.code == 200) {
									this.$refs.uToast.show({
										title: '操作成功',
										type: 'success'
									})
									//#ifdef MP-WEIXIN
									wx.stopLocationUpdate({
										success: (res) => {
											console.log('关闭监听实时位置变化成功:', res)
										},
										fail: (res) => {
											console.log('关闭监听实时位置变化失败:', res)
										}
									})

									let appYingyanTimeId = uni.getStorageSync('appYingyanTimeId')
									if (typeof appYingyanTimeId != 'undefined' && appYingyanTimeId !=
										null && appYingyanTimeId != '') {
										clearInterval(appYingyanTimeId)
										uni.removeStorageSync('appYingyanTimeId')
										uni.removeStorageSync('appYingyanDispatchNumber')
									}

									// 清空 司机车牌-货运订单 缓存
									uni.removeStorageSync(carnumbger + "-" + dispatchNumber)

									uni.removeStorageSync(dispatchNumber)

									uni.request({
										method: "POST",
										url: config.root + '/rest/track/updateTrace',
										params: {
											traceName: carnumbger + "-" + dispatchNumber
										},
										header: {
											'Authorization': uni.getStorageSync('token'),
										},
										success: (res) => {
											// console.log("结束轨迹成功")
										}
									})

									//#endif
									setTimeout(() => {
										this.refresh()
									}, 500)
								} else {
									this.$refs.uToast.show({
										title: res.data.message,
										type: 'error'
									})
									setTimeout(() => {
										this.refresh()
									}, 500)
								}
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				})
			},
			// 自助打印
			selfPrint(item) {
				this.barCode = true;
				this.options.code = item.dispatchNumber;
				this.options.text.content = item.dispatchNumber;
			},
			toyunshu(dispatchNumber) {
				this.$Router.push({
					name: 'transport_msg',
					params: {
						dispatchNumber: dispatchNumber
					}
				})
			},
			tojypj(dispatchNumber) {
				this.$Router.push({
					name: 'tDispatchDriverEval',
					params: {
						dispatchNumber: dispatchNumber
					}
				})
			},
			xuznekehu(item) {
				uni.showModal({
					content: '确定送达吗？',
					success: (res) => {
						if (res.confirm) {
							var that = this
							uni.getLocation({
								type: 'gcj02',
								altitude: false,
								success: function(res) {
									const latitude = res.latitude;
									const longitude = res.longitude;
									uni.request({
										method: "POST",
										header: {
											'Authorization': uni.getStorageSync('token'),
										},
										url: config.root +
											'/rest/transportCar/editsongdajiaoyan?longitude=' +
											longitude + '&latitude=' + latitude +
											'&getUnitId=' + item.getUnitId + '&getUnit=' +
											item.getUnit + '&getLocationId=' + item
											.getLocationId,
										data: {},
										success: (res) => {
											if (res.data.code == 200) {
												uni.request({
													method: "POST",
													header: {
														'Authorization': uni
															.getStorageSync(
																'token'),
													},
													url: config.root +
														'/rest/transportCar/editsongda?dispatchNumber=' +
														item.dispatchNumber +
														'&longitude=' +
														longitude +
														'&latitude=' +
														latitude +
														'&getUnitId=' + item
														.getUnitId +
														'&getUnit=' + item
														.getUnit +
														'&getLocationId=' +
														item.getLocationId +
														'&getLocation=' + item
														.getLocation,
													data: {},
													success: (res) => {
														if (res.data
															.code == 200) {
															uni.showToast({
																title: '送达成功',
																duration: 2000
															});
															setTimeout(
																() => {
																	that.refresh()
																}, 500)
															that.show =
																false
														} else {
															uni.showToast({
																title: res
																	.data
																	.message,
																duration: 2000,
																icon: 'none',
															});
														}
													}
												})
											} else {
												uni.showModal({
													content: '您所在位置超出收货范围，是否提交地址修改申请？',
													success: (res) => {
														if (res.confirm) {
															uni.request({
																method: "POST",
																header: {
																	'Authorization': uni
																		.getStorageSync(
																			'token'
																		),
																},
																url: config
																	.root +
																	'/rest/transportCar/addressEditapplyfor?dispatchNumber=' +
																	item
																	.dispatchNumber +
																	'&longitude=' +
																	longitude +
																	'&latitude=' +
																	latitude +
																	'&getUnitid=' +
																	item
																	.getUnitId +
																	'&getUnit=' +
																	item
																	.getUnit +
																	'&getLocationid=' +
																	item
																	.getLocationId,
																data: {},
																success: (
																	res
																) => {
																	if (res
																		.data
																		.code ==
																		200
																	) {
																		uni.showToast({
																			title: '提交成功，请等待审核后重试',
																			duration: 2000
																		});
																		that.show =
																			false
																	} else {
																		uni.showToast({
																			title: res
																				.data
																				.message,
																			duration: 2000,
																			icon: 'none',
																		});
																	}
																}
															})
														}
													},
												})
											}
										}
									})
								},
								fail: function(res) {
									uni.showToast({
										title: '获取定位信息失败，请重试',
										duration: 2000,
										icon: 'none'
									});
									that.show = false
								},
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				})
			},
			songda(item) {
				this.$Router.push({
					name: 'transport_reachlist',
					params: {
						dispatchNumber: item.dispatchNumber,
						type: '1',
					},
				})
				/* let arr = []
				for(let item1 of item.list){
					let obj = {}
					obj.dispatchNumber = item.dispatchNumber
					obj.getUnitId = item1.getUnitId
					obj.getUnit = item1.getUnit
					obj.getLocationId = item1.getLocationId
					obj.getLocation = item1.getLocation
					obj.carnumbger = item.carnumbger
					arr.push(obj)
				}
				this.clist = arr
				this.show = true */
			},
			lichangfayun(dispatchNumber, carnumbger, driver1, cartel, guiji) {
				uni.showModal({
					content: '确定发运吗？',
					success: (res) => {
						if (res.confirm) {
							this.$http.post('/rest/transportCar/getTs', {}, {
								params: {
									dispatchNumber: dispatchNumber
								},
								header: {
									'Authorization': uni.getStorageSync('token'),
								}
							}).then(res => {
								if (res.data.code == 200) {
									this.fayun(dispatchNumber);
								} else {
									console.log(res);
									uni.showModal({
										title: '提示',
										content: res.data.message,
										success: (res) => {
											if (res.confirm) {
												this.fayun(dispatchNumber);
											} else if (res.cancel) {
												console.log('用户点击取消');
											}
										}
									});
								}
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				})
			},
			fayun(dispatchNumber) {
				uni.showLoading({
					title: '正在提交'
				})
				this.$http.post('/rest/transportCar/editlicahng', {}, {
					params: {
						dispatchNumber: dispatchNumber
					},
					header: {
						'Authorization': uni.getStorageSync('token'),
					}
				}).then(res => {
					uni.hideLoading()
					if (res.data.code == 200) {
						uni.showToast({
							title: '发运成功',
							duration: 2000
						});
						setTimeout(() => {
							this.refresh()
						}, 500)
					} else {
						uni.showModal({
							title: '操作失败!',
							content: res.data.message,
							showCancel: false,
							success: (res) => {
								if (res.confirm) {} else if (res.cancel) {
									console.log('用户点击取消');
								}
							}
						});
					}
				})
			},
			daochangzhuangche(item) {
				uni.showModal({
					content: '确定到仓吗？',
					success: (res) => {
						if (res.confirm) {
							var that = this
							uni.getLocation({
								type: 'gcj02',
								altitude: false,
								success: function(res) {
									const latitude = res.latitude;
									const longitude = res.longitude;
									uni.request({
										method: "POST",
										header: {
											'Authorization': uni.getStorageSync('token'),
										},
										url: config.root +
											'/rest/transportCar/editdaocangjiaoyan?longitude=' +
											longitude + '&latitude=' + latitude +
											'&setLocationId=' + item.list[0]
											.setLocationId + '&operateId=' + item
											.operateId,
										data: {},
										success: (res) => {
											if (res.data.code == 200) {
												var openId = wx.getStorageSync(
													'openId')
												uni.request({
													method: "POST",
													header: {
														'Authorization': uni
															.getStorageSync(
																'token'),
													},
													url: config.root +
														'/rest/transportCar/editdaocang?dispatchNumber=' +
														item.dispatchNumber +
														'&longitude=' +
														longitude +
														'&latitude=' +
														latitude +
														'&setLocationId=' +
														item.list[0]
														.setLocationId +
														'&openId=' + openId,
													data: {},
													success: (res) => {
														if (res.data
															.code == 200) {
															uni.showToast({
																title: '操作成功',
																duration: 2000
															});
															setTimeout(
																() => {
																	that.refresh()
																}, 500)
														} else {
															uni.showToast({
																title: res
																	.data
																	.message,
																duration: 2000,
																icon: 'none',
															});
														}
													}
												})
											} else {
												uni.showModal({
													title: '提示',
													content: res.data.message,
													showCancel: false,
													success: (res) => {}
												});
											}
										}
									})
								},
								fail: function(res) {
									uni.showModal({
										title: '提示',
										content: '获取位置失败，请重试',
										showCancel: false,
										success: (res) => {}
									});
								},
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				})
			},
			refresh() {
				uni.showLoading({
					title: '加载中...'
				})
				if (this.current == 0) {
					this.$http.post('/rest/transportCar/findCarListBySpecToo2', {}, {
						params: {
							findtype: '1'
						},
						header: {
							'Authorization': uni.getStorageSync('token'),
						}
					}).then(res => {
						this.dataList = res.data.data
						this.jiaoyan(res.data.data)
					})
				} else if (this.current == 1) {
					this.pageNo = 1
					this.hasMore = true
					this.loadOrder({
						findtype: '1'
					}).then(res => {})
				}
				uni.hideLoading()
			},
			loadOrder(param = {}) {
				return new Promise((resolve, reject) => {
					param = Object.assign(param, {
						page: this.pageNo,
						limit: 5
					})
					this.$http.post('/rest/transportCar/findCarListBySpecToo3', {}, {
						params: param,
						header: {
							'Authorization': uni.getStorageSync('token'),
						}
					}).then(res => {
						if (this.pageNo == 1) {
							this.dataList2 = res.data.data
						} else {
							this.dataList2 = this.dataList2.concat(res.data.data)
						}
						this.orderCount = res.data.count
						this.hasMore = this.pageNo * 5 < res.data.count
						resolve(res)
					})
				})
			},
			jiaoyan(data) {
				//查询运输中运单  进行上传轨迹
				//#ifdef MP-WEIXIN

				let appYingyanTimeId = uni.getStorageSync('appYingyanTimeId')
				if (typeof appYingyanTimeId != 'undefined' && appYingyanTimeId != null && appYingyanTimeId != '') {
					if (data.length == 0) {
						wx.stopLocationUpdate({
							success: (res) => {
								console.log('关闭监听实时位置变化成功:', res)
							},
							fail: (res) => {
								console.log('关闭监听实时位置变化失败:', res)
							}
						})

						let appYingyanTimeId = uni.getStorageSync('appYingyanTimeId')
						if (typeof appYingyanTimeId != 'undefined' && appYingyanTimeId != null && appYingyanTimeId != '') {
							clearInterval(appYingyanTimeId)
							uni.removeStorageSync('appYingyanTimeId')
							uni.removeStorageSync('appYingyanDispatchNumber')
						}
						return
					} else {
						let appYingyanDispatchNumber = uni.getStorageSync('appYingyanDispatchNumber')
						if (data[0].dispatchNumber != appYingyanDispatchNumber) {
							wx.stopLocationUpdate({
								success: (res) => {
									console.log('关闭监听实时位置变化成功:', res)
								},
								fail: (res) => {
									console.log('关闭监听实时位置变化失败:', res)
								}
							})

							let appYingyanTimeId = uni.getStorageSync('appYingyanTimeId')
							if (typeof appYingyanTimeId != 'undefined' && appYingyanTimeId != null && appYingyanTimeId !=
								'') {
								clearInterval(appYingyanTimeId)
								uni.removeStorageSync('appYingyanTimeId')
								uni.removeStorageSync('appYingyanDispatchNumber')
							}
						} else {
							return
						}
					}
				}
				if (data.length > 0) {
					const latestItem = data[data.length - 1];

					let shangchuan = false
					for (let item1 of data) {
						if (item1.tstate == '运输中' || item1.tstate == '待发运' || item1.tstate == '已定价') {
							var that = this
							let guiji = item1.guiji
							if (item1.paiType == 'web') { //更新

								let switchWl = false
								for (let item2 of item1.list) {
									if (item2.getLocation != null && item2.getLocation != '') {
										this.$http.post('/rest/address/getByid', {}, {
											params: {
												id: item2.getLocationId
											},
											header: {
												'Authorization': uni.getStorageSync('token'),
											}
										}).then(res => {
											if (res.data.data.switchWl == 1) {
												if (switchWl == false) {
													// 保证最新的运单进行轨迹创建
													if (item1.dispatchNumber === latestItem.dispatchNumber) {
														uni.request({
															method: "POST",
															url: config.root +
																'/rest/track/checkTerminals?carnumbger=' +
																item1.carnumbger + '&dispatchNumber=' +
																item1.dispatchNumber,
															data: {},
															header: {
																'Authorization': uni.getStorageSync(
																	'token'),
															},
															success: (res) => {
																// console.log("创建终端成功:",res)
																uni.setStorageSync(item1.carnumbger +
																	"-" + item1.dispatchNumber, res
																	.data)

																if (res.data.trid != null && res.data
																	.trid !== '') {
																	uni.request({
																		method: "POST",
																		url: config.root +
																			'/rest/transport/edittDispatch?dispatchNumber=' +
																			item1
																			.dispatchNumber,
																		data: {},
																		header: {
																			'Authorization': uni
																				.getStorageSync(
																					'token'),
																		},
																		success: (res) => {
																			if (res.data
																				.code ==
																				200) {
																				//成功更新
																				switchWl =
																					true
																				guiji = '1'
																			}
																		}
																	})
																}
															},
															fail: (err) => {}
														})
													}
												}
											}
										})
									}
								}

								uni.request({
									method: "POST",
									url: config.root + '/rest/transport/edittDispatch2?dispatchNumber=' + item1
										.dispatchNumber,
									data: {},
									header: {
										'Authorization': uni.getStorageSync('token'),
									},
									success: (res) => {
										if (res.data.code == 200) {
											//成功更新
										}
									}
								})
							}

							if (item1.dispatchNumber === latestItem.dispatchNumber) {
								let trackDataArray = uni.getStorageSync(item1.dispatchNumber) || [];
								// 将初始的 trackDataArray 存储到本地存储中
								uni.setStorageSync(item1.dispatchNumber, trackDataArray);

								if (!shangchuan) {
									wx.startLocationUpdateBackground({
										success: (res) => {
											let appYingyanTimeId = setInterval(() => {
												uni.getLocation({
													type: 'gcj02',
													altitude: false,
													success: function(res) {
														if (guiji == '1') {
															let trackData = uni
																.getStorageSync(item1
																	.carnumbger + "-" +
																	item1.dispatchNumber);
															// console.log("trackData:", trackData);
															let sid = trackData.sid;
															let tid = trackData.tid;
															let trid = trackData.trid;

															// 创建新的定位点
															let newPoint = {
																"location": res
																	.longitude + ',' +
																	res.latitude,
																"locatetime": Math
																	.round(new Date()
																		.getTime()),
																"height": res
																	.verticalAccuracy,
																"accuracy": res
																	.accuracy
															};

															// 从本地存储中读取最新的 trackDataArray
															trackDataArray = uni
																.getStorageSync(item1
																	.dispatchNumber) || [];
															trackDataArray.push(newPoint);
															// 存储更新后的 trackDataArray
															uni.setStorageSync(item1
																.dispatchNumber,
																trackDataArray);

															// console.log("trackDataArray:",trackDataArray)
															if (trackDataArray.length >=
																30) {
																uni.request({
																	method: "POST",
																	url: config
																		.root +
																		'/rest/track/uploadTracePointLocal',
																	data: {
																		sid: sid,
																		tid: tid,
																		dispatchNumber: item1.dispatchNumber,
																		points: trackDataArray
																	},
																	header: {
																		'Authorization': uni
																			.getStorageSync(
																				'token'
																			),
																	},
																	success: (
																		res
																	) => {
																		// console.log("上传轨迹点成功")
																		trackDataArray
																			= [];
																		uni.setStorageSync(
																			item1
																			.dispatchNumber,
																			trackDataArray
																		);
																	}
																})
															}

														}
													},
													fail: function(res) {
														//获取当前位置失败
													},
												})
											}, 500)
											shangchuan = true
											uni.setStorageSync('appYingyanTimeId', appYingyanTimeId);
											uni.setStorageSync('appYingyanDispatchNumber', item1
												.dispatchNumber);
										},
										fail: (res) => {
											const msg = '开启持续定位失败。请在我的-定位授权中开启使用小程序期间和离开小程序后位置权限';
											uni.showToast({
												icon: 'none',
												title: msg,
												duration: 4000
											});
										}
									})
								}
							}
						}
					}
				}
				//#endif
			},
			huidan(dispatchNumber) {
				this.$Router.push({
					name: 'transport_receipt',
					params: {
						dispatchNumber: dispatchNumber,
						type: '1',
					}
				})
			},
			badaphone(tel) {
				if (tel == null || tel == '') {
					uni.showToast({
						title: "暂无联系电话",
						icon: 'none'
					})
				} else {
					uni.makePhoneCall({
						phoneNumber: tel
					});
				}
			},
			tooCarsy(dispatchNumber) {
				this.$Router.push({
					name: 'checktooy',
					params: {
						dispatchNumber: dispatchNumber
					}
				})
			},
			uploadimg(dispatchNumber) {
				uni.chooseImage({
					count: 1, //默认9
					success: res1 => {
						var that = this
						uni.uploadFile({
							url: config.root + 'rest/transportCar/uploadzhuangzaiFile', // 文件上传路径
							filePath: res1.tempFilePaths[0],
							name: 'file',
							header: {
								'Authorization': uni.getStorageSync('token'),
							},
							formData: {
								dispatchNumber: dispatchNumber
							},
							success: res => {
								let data = JSON.parse(res.data);
								if (data.code == 200) {
									uni.showToast({
										title: '上传成功',
										duration: 2000
									});
									setTimeout(() => {
										that.refresh()
									}, 500)
								} else {
									uni.showToast({
										title: data.message,
										duration: 2000,
										icon: 'none',
									});
								}
							}
						})
					}
				})
			},
			sectionChange(e) {
				this.current = e
				this.refresh()
			},
		}
	}
</script>

<style lang="scss">
	.u-body-item {
		padding: 20rpx 15rpx;
	}

	.wrap {
		padding: 0rpx 12rpx;
	}

	.u-row {
		margin: 10rpx 2rpx;
	}

	.weikaishi2 {
		font-size: 38rpx;
	}

	.baise {
		color: #ffffff;
		font-size: 34rpx;
	}
	.qrcode {
		height: 460rpx;
		display: flex;
		flex-direction: column;
		justify-content: flex-end;
		align-items: center;
	}

	#qrcode {
		width: 460rpx;
		height: 460rpx;
		background-color: #fff;
	}
</style>
